//
//  LogisticsVC.m
//  代理端
//
//  Created by sliver on 15/12/15.
//  Copyright (c) 2015年 sliver. All rights reserved.
//

#import "LogisticsVC.h"
#import "Unit.h"
#import "WXDataService.h"
#import "SBJson.h"
#import "headProductView.h"
#import "footView.h"
@interface LogisticsVC ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableDictionary *dataDic;  //订单    
    NSMutableArray *dataAry; //物流

}
@property(nonatomic,strong)UITableView *tableview;
@end

@implementation LogisticsVC
-(NSMutableArray *)wuliumodel
{
    if (!_wuliumodel) {
        _wuliumodel = [NSMutableArray array];
    }
    return _wuliumodel;
}
-(UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64*KScr+headProductViewHeight, ScreenW, 340*KScr)];
        
        _tableview.delegate = self;
        
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    }

    return _tableview;


}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    dataDic = [[NSMutableDictionary alloc]init];
    dataAry = [[NSMutableArray alloc]init];
//    [self setupUi];
    [self loadNav];
    [self loadData];
//    [self loadData];
     // [self loadNav];
    // Do any additional setup after loading the view.
}

-(void)setupUi:(NSInteger)inter
{
    headProductView *headview = (headProductView*)[[[NSBundle mainBundle]loadNibNamed:@"headProductView" owner:nil options:nil]lastObject];
    headview.frame= CGRectMake(0, 64, ScreenW, headProductViewHeight);
    [headview gaveMeModel:self.model];
    footView *footview = [[[NSBundle mainBundle]loadNibNamed:@"footView" owner:nil options:nil]lastObject];

    if (inter *44*KScr+50*KScr<=340*KScr) {
        self.tableview.frame =CGRectMake(0, 64*KScr+headProductViewHeight, ScreenW, inter*44*KScr+50*KScr);
        self.tableview.bounces = NO;

    }else
    {
        self.tableview.frame = CGRectMake(0, 64*KScr+headProductViewHeight, ScreenW, 340*KScr);
    
    }
    
    
    footview.frame = CGRectMake(0, CGRectGetMaxY(self.tableview.frame), ScreenW, footViewHeight) ;
    [footview gaveMeModel:self.model];
    [self.view addSubview:headview];
    [self.view addSubview:footview];
    [self.view addSubview:self.tableview];


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.wuliumodel.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    WuLiuModel *model = self.wuliumodel[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14*KScr];
    cell.textLabel.text =model.AcceptStation;
    cell.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 50*KScr;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}// Default is 1 if not implemented

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"物流信息";
}// fixed font style. use custom view (UILabel) if you want something different

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WuLiuModel *model = self.wuliumodel[indexPath.row];
    
    [BackView addToview:self.view WithDeleget:self WithMessage:model.AcceptStation WithBtnCount:1];
}

#pragma mark 订单详情
-(void)loadData{
    
    [MyMbd addHudToView:self.view];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    
//    NSLog(@"%@",_order_id);
    [params setObject:_order_id forKey:@"id_"];
    [WXDataService requestWithURL:@"mobile/order/getOrder.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
//        [MyMbd hidenHudFromView:self.view];
//        NSLog(@"订单详情：%@",result);
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            OrderDetailModel *model = [[OrderDetailModel alloc]initWithDataDic:result[@"order"]];
            self.model = model;
            
//            dataDic = result[@"order"];
            [self postWuLiuNetWorking];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MyMbd hidenHudFromView:self.view];
    }];
    //物流
    
}

#pragma mark 物流信息
-(void)postWuLiuNetWorking
{
    NSMutableDictionary *params2 = [[NSMutableDictionary alloc]init];
    [params2 setObject:_order_id forKey:@"order_id_"];
    [WXDataService requestWithURL:@"mobile/kd/logisticsInformation.jhtml" params:params2 httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        [MyMbd hidenHudFromView:self.view];
        
//        NSLog(@"物流信息：%@",result);
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            dataAry= result[@"traces"][@"listItems"];
            for (NSDictionary *dic in dataAry) {
                WuLiuModel *model = [[WuLiuModel alloc]initWithDataDic:dic];
                [self.wuliumodel addObject:model];
            }
//            NSInteger inter =  dataAry.count*44*KScr;
            
            
            
//            [self loadmainView];
            [self setupUi:dataAry.count];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MyMbd hidenHudFromView:self.view];
    }];


}
-(void)loadmainView{
    //viewk;
    UIView *viewback= [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 385)];
    viewback.backgroundColor = [UIColor whiteColor];
//    self.bView = viewback;
    [self.view addSubview:viewback];
    //商品名称
    UILabel *shopnameLab = [[UILabel alloc]initWithFrame:CGRectMake(20, 65, 240, 20)];
    shopnameLab.text = dataDic[@"commodity_name"];
    shopnameLab.font = [UIFont boldSystemFontOfSize:17];
    [viewback addSubview:shopnameLab];
    //订单时间
    NSArray *ary = [dataDic[@"timestamp"] componentsSeparatedByString:@" "];
    UILabel *tinmeLab = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW -90, 65, 80, 20)];
    tinmeLab.text = ary[0];
    tinmeLab.font = [UIFont systemFontOfSize:13];
    [viewback addSubview:tinmeLab];
    //类别
    NSArray *jsonAry = [dataDic[@"properties"] JSONValue];
    for (int i=0; i<jsonAry.count; i++) {
        UILabel *leibieLab = [[UILabel alloc]initWithFrame:CGRectMake(20+80*i, 90, 80, 20)];
        leibieLab.text = [NSString stringWithFormat:@"%@: %@",jsonAry[i][@"name" ],jsonAry[i][@"value"]];
        leibieLab.font = [UIFont systemFontOfSize:13];
        [viewback addSubview:leibieLab];
    }
    //价格
    UILabel *priceLab = [[UILabel alloc]initWithFrame:CGRectMake(20, 115, 180, 20)];
    priceLab.text =[NSString stringWithFormat:@"￥%@/件",dataDic[@"univalent"]];
    priceLab.font = [UIFont systemFontOfSize:15];
    [viewback addSubview:priceLab];
    //数量
    UILabel *numLab = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW - 60, 115, 50, 20)];
    numLab.textAlignment = NSTextAlignmentRight;
    numLab.font = [UIFont systemFontOfSize:15];
    numLab.text = [NSString stringWithFormat:@"x%@",dataDic[@"count"]];
    [viewback addSubview:numLab];
    //合计
    UILabel *hejilab = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW - 150, 140, 140, 20)];
    hejilab.text =[NSString stringWithFormat:@"合计：%i",[dataDic[@"count"] intValue] * [dataDic[@"univalent"] intValue]];
    hejilab.textAlignment = NSTextAlignmentRight;
    hejilab.font = [UIFont boldSystemFontOfSize:17];
    [viewback addSubview:hejilab];
    UIView *viee = [[UIView alloc]initWithFrame:CGRectMake(0, 165, ScreenW, 10)];
    viee.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    [viewback addSubview:viee];
    //物流
    UILabel *nameLab2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 180, 110, 20)];
    nameLab2.text = @"物流信息";
    nameLab2.font = [UIFont boldSystemFontOfSize:17];
    [viewback addSubview:nameLab2];
   //灰线
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 205, ScreenW, 1)];
    view1.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    [viewback addSubview:view1];
    //scroller
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 210, ScreenW, ScreenH-210)];
    scroll.backgroundColor = [UIColor whiteColor];
    scroll.contentSize = CGSizeMake( 0, dataAry.count *60 +80);
    [self.view addSubview:scroll];
    for (int i =0; i<dataAry.count; i++) {
        UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 60*i, ScreenW - 110, 40)];
        lab1.text =dataAry[i][@"AcceptStation"];
        lab1.numberOfLines = 0;
        lab1.font = [UIFont systemFontOfSize:12];
        [scroll addSubview:lab1];
        
        UILabel *timeLab = [[UILabel alloc]initWithFrame:CGRectMake(100, 60*i +40, ScreenW-100, 20)];
        timeLab.text = dataAry[i][@"AcceptTime"];
        timeLab.font = [UIFont systemFontOfSize:12];
        [scroll addSubview:timeLab];
        
        UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(90, 60+60*i, ScreenW, 1)];
        view2.backgroundColor = DRINK_LIGHTGRAY_COLOR;
        [scroll addSubview:view2];
    }
    
    UIView *view5 = [[UIView alloc]initWithFrame:CGRectMake(0, 60*dataAry.count, ScreenW, 1)];
    view5.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    [scroll addSubview:view5];
    //物流公司                                       
    UILabel *nameLab3 = [[UILabel alloc]initWithFrame:CGRectMake(15, 60*dataAry.count+5, 110, 20)];
    nameLab3.text = dataDic[@"logistics_company"];
    nameLab3.font = [UIFont boldSystemFontOfSize:17];
    [scroll addSubview:nameLab3];
    //物流单号
    UILabel *nameLab5 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW - 120, 60*dataAry.count+5, 105, 20)];
    nameLab5.text =[NSString stringWithFormat:@"%@",dataDic[@"logistics_airway_bill"]];
    nameLab5.font = [UIFont boldSystemFontOfSize:13];
    nameLab5.textAlignment = NSTextAlignmentRight;
    [scroll addSubview:nameLab5];
    [self loadNav];
}
#pragma mark - nav
-(void)loadNav{
    self.view.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 20)];
    redview.backgroundColor = DRINK_RED_COLOR;
//    [redview bringSubviewToFront:self.bView];
    [self.view addSubview:redview];
    UILabel *redLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenW, 40)];
    redLab.backgroundColor = DRINK_RED_COLOR;
    redLab.text = @"物流详情";
    redLab.textColor = [UIColor whiteColor];
    redLab.textAlignment =  NSTextAlignmentCenter;
    redLab.font = [UIFont boldSystemFontOfSize:19];
    [self.view addSubview:redLab];
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20,44, 40)];
    backBtn.adjustsImageWhenHighlighted = NO;
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
}
-(void)clickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
